package com.mapapp.flutter_mapapp.activity;

import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.mapapp.flutter_mapapp.R;
import com.mapapp.flutter_mapapp.adapter.HistoryAdapter;
import com.mapapp.flutter_mapapp.bean.OperationBean;
import com.mapapp.flutter_mapapp.persenter.HistoryPresenter;
import com.mapapp.flutter_mapapp.util.ToastUtils;
import com.mapapp.flutter_mapapp.view.HistoryViewInterface;
import com.mapapp.flutter_mapapp.widget.TitleBar;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class HistoryActivity extends ImmersiveBaseActivity implements HistoryViewInterface {

    @BindView(R.id.rv_history)
    RecyclerView rvHistory;
    @BindView(R.id.title_bar)
    TitleBar titleBar;

    private HistoryPresenter presenter;

    private HistoryAdapter historyAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);
        ButterKnife.bind(this);
        String regionId = getIntent().getStringExtra("regionId");
        String householdName = getIntent().getStringExtra("householdName");
        presenter = new HistoryPresenter(this, this, lifecycleSubject);


        titleBar.getTitleBarCenter().setText(householdName);

        historyAdapter = new HistoryAdapter(R.layout.item_history);
        rvHistory.setLayoutManager(new LinearLayoutManager(mContext));
        rvHistory.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
            }
        });
        rvHistory.setAdapter(historyAdapter);

        historyAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                OperationBean operationBean = historyAdapter.getData().get(position);
                Intent intent = new Intent(mContext, HistoryDetailActivity.class);
                intent.putExtra("opId", operationBean.getId());
                startActivity(intent);
            }
        });

        titleBar.getIvBack().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        presenter.getHistory(regionId);

    }

    @Override
    public void showHistory(List<OperationBean> operationBeanList) {
        historyAdapter.setNewData(operationBeanList);
        if (historyAdapter.getData().isEmpty()) {
            ToastUtils.showMessage(mContext, "暂无数据");
        }
    }
}
